package com.ea.eadp.nexus.test.common;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * User: woodywu
 */
public class PageOperation {
	public static void SelectCheckbox(WebDriver driver, String elementId, String checkBoxLabelId, boolean check) {
		if (check && !driver.findElement(By.id(elementId)).isSelected()) {
			driver.findElement(By.id(checkBoxLabelId)).click();
		} else if (!check && driver.findElement(By.id(elementId)).isSelected()) {
			driver.findElement(By.id(checkBoxLabelId)).click();
		}
	}

	public static void SelectCheckboxAfterWait(WebDriver driver, String elementId, String checkBoxLabelId, boolean check) {
		WebDriverWait(driver,checkBoxLabelId);
		SelectCheckbox(driver, elementId, checkBoxLabelId, check);
	}

/*	public static void SelectCssSelectorItemAfterWait(WebDriver driver, String cssSelector, String value) throws Exception {
        driver.findElement(By.cssSelector(cssSelector)).click();
        String cssSelectorValue = String.format("a[value=\"%s\"] > span", value);
        WebDriverWaitByCssSelector(driver, cssSelectorValue);
		driver.findElement(By.cssSelector(cssSelectorValue)).click();
	}*/

    public static void SelectCssDropDownItem(WebDriver driver, String controlId, String item) throws Exception{
        item = item.replaceAll("'","\\\\'").replaceAll("\"","\\\\\"");
        driver.findElement(By.cssSelector(controlId + " > div.origin-ux-drop-down-selection > span")).click();
        String cssSelectorValue = String.format("a[value=\"%s\"] > span:visible", item);
        WebDriverWaitByCssSelector(driver, controlId + " > div.drop-down-options-container");
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("$('" + cssSelectorValue + "').click()");
    }


	public static void SendKeyToElement(WebDriver driver, String elementId, String key) {
		driver.findElement(By.id(elementId)).clear();
		driver.findElement(By.id(elementId)).sendKeys(key);
	}

	public static void SendKeyToElementAfterWait(WebDriver driver, String elementId, String key) {
		WebDriverWait(driver, elementId);
		SendKeyToElement(driver, elementId, key);
	}

	public static void SendKeyToElementByPath(WebDriver driver, String elementXpath, String key) {
		driver.findElement(By.xpath(elementXpath)).clear();
		driver.findElement(By.xpath(elementXpath)).sendKeys(key);
	}

	public static void WebDriverWait(WebDriver driver, String webElementId) {
		WebDriverWait wait = new WebDriverWait(driver, 30, 1000);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(webElementId)));
	}

	public static void WebDriverWaitByXpath(WebDriver driver, String webElementXpath) {
		WebDriverWait wait = new WebDriverWait(driver, 30, 1000);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(webElementXpath)));
	}

	public static void WebDriverWaitByCssSelector(WebDriver driver, String cssSelector) {
		WebDriverWait wait = new WebDriverWait(driver, 30, 1000);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(cssSelector)));
	}

    public static void WebDriverWaitCurrentUrlContains(WebDriver driver, final String url) {
        WebDriverWait wait = new WebDriverWait(driver, 10, 1000);
        wait.until(
                new ExpectedCondition<Boolean>() {
                    public Boolean apply(WebDriver driver) {
                        return TestUtil.getCurrentUrl(driver).contains(url);
                    }
                });
    }

}
